home *** CD-ROM | disk | FTP | other *** search
/ Java Interactive Reference Guide / Java Interactive Reference Guide.iso / autorun / source.dir / 00116_15.txt < prev    next >
Encoding:
Text File  |  1980-01-11  |  3.6 KB  |  156 lines

  1. import java.lang.*;
  2. import awt.*;
  3. import nathanw.*;
  4.  
  5. class TetrisGame implements PosPaintable {
  6.     Grid playfield;
  7.     TetrisPiece piece;
  8.     int curScore;
  9.     int px,py; 
  10.     intNonZero testNonZero;
  11.     intOr      putDown;
  12.  
  13.     public TetrisGame(int xs,int ys,boolean randomCrap)
  14.     {
  15.     playfield=new Grid(xs,ys,1);
  16.     clear();
  17.     piece = new TetrisPiece();
  18.     py=0;
  19.     px=(xs-piece.sizex())/2;
  20.     testNonZero = new intNonZero();
  21.     putDown = new intOr();
  22.     curScore=0;
  23.     }
  24.     
  25.     void clear()
  26.     {
  27.     playfield.fill(0,0,playfield.sizex(),playfield.sizey(),0);
  28.     }
  29.  
  30.     public void paint(Graphics g, int x,int y)
  31.     {
  32.     g.drawRect(x,y,2+playfield.sizex()*10,2+playfield.sizey()*10);
  33.     for(int j=0;j<playfield.sizey();j++)
  34.         for(int i=0;i<playfield.sizex();i++)
  35.         if((playfield.grid[i][j]!=0) ||
  36.            ((i>=px) && (i<px+piece.sizex()) && 
  37.             (j>=py) && (j<py+piece.sizey()) && 
  38.             (piece.grid[i-px][j-py]!=0)))
  39.             g.fillRect(3+x+i*10,3+y+j*10,8,8);
  40.         }
  41.  
  42.     public void unpaint_dynamic(Graphics g, int x,int y)
  43.     {
  44.     Color fg;
  45.     fg=g.foreground;
  46.     g.setForeground(g.background);
  47.     for(int j=0;j<playfield.sizey();j++)
  48.         for(int i=0;i<playfield.sizex();i++)
  49.         if(((i>=px) && (i<px+piece.sizex()) && 
  50.             (j>=py) && (j<py+piece.sizey()) && 
  51.             (piece.grid[i-px][j-py]!=0)))
  52.             g.fillRect(3+x+i*10,3+y+j*10,8,8);
  53.         g.setForeground(fg);
  54.     }    
  55.  
  56.  
  57.     public boolean step()
  58.     {
  59.     if(playfield.compare(piece,px,py+1,testNonZero))
  60.         {
  61.         // Put down the piece
  62.         playfield.put_on(piece,px,py,putDown);
  63.         // Clear out full lines
  64.         for(int j=playfield.sizey()-1;j>=0;j--)
  65.         while(testFullLine(j)==true)
  66.             {
  67.             deleteLine(j);
  68.             // Simple scoring function
  69.             curScore+=10;
  70.             }
  71.  
  72.             // Put on a new piece
  73.         piece = new TetrisPiece();
  74.         py=0;
  75.         px=(playfield.sizex()-piece.sizex())/2;
  76.         if(playfield.compare(piece,px,py,testNonZero))
  77.         return true;
  78.             }
  79.         py++;
  80.     return false;
  81.         }
  82.  
  83.     private boolean testFullLine(int y)
  84.     {
  85.     for(int i=0;i<playfield.sizex();i++)
  86.         if(playfield.grid[i][y]==0)
  87.         return false;
  88.         return true;
  89.     }
  90.  
  91.     private void deleteLine(int y)
  92.     {
  93.     for(int j=y;j>0;j--)
  94.             for(int i=0;i<playfield.sizex();i++)
  95.         playfield.grid[i][j]=playfield.grid[i][j-1];
  96.         for(int i=0;i<playfield.sizex();i++)
  97.         playfield.grid[i][0]=0;
  98.         }
  99.  
  100. // Data-returning methods
  101.  
  102.     public int score()
  103.     {
  104.     return curScore;
  105.     }
  106.  
  107. // Game-play interface methods
  108.  
  109.     public void move_left(int i)
  110.     {
  111.     if(playfield.compare(piece,px-i,py,testNonZero))
  112.         return; // Should we throw an exception here?
  113.         px-=i;
  114.     }
  115.     public void move_left()
  116.     {
  117.         move_left(1);
  118.     }
  119.  
  120.     public void move_right(int i)
  121.     {
  122.     if(playfield.compare(piece,px+i,py,testNonZero))
  123.         return; // Should we throw an exception here?
  124.         px+=i;
  125.     }
  126.     public void move_right()
  127.     {
  128.         move_right(1);
  129.     }
  130.  
  131.     public void rotate_cw()
  132.     {
  133.     TetrisPiece test=(TetrisPiece) piece.clone();
  134.         test.rotate_cw();
  135.     if(!playfield.compare(test,px,py,testNonZero))
  136.         piece=test;
  137.         }
  138.  
  139.     public void rotate_ccw()
  140.     {
  141.     TetrisPiece test=(TetrisPiece) piece.clone();
  142.         test.rotate_ccw();
  143.     if(!playfield.compare(test,px,py,testNonZero))
  144.         piece=test;
  145.         }
  146.  
  147.     public void drop()
  148.     {
  149.     while(!playfield.compare(piece,px,py+1,testNonZero))
  150.         py++;
  151.         }
  152. }
  153.  
  154.  
  155.  
  156.